﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Xml.Serialization;
using ComLib.ComFun;
using ComLib.ConfigService;

namespace KAImage.Config
{
    [Serializable]
    [XmlRootAttribute("ImageCurveOption", Namespace = "KAImage.Config", IsNullable = false)]
    public class ImageCurveOption : XmlSerializeBase
    {     
        /// <summary>
        /// 全图
        /// </summary>
        public ChannelCurveOption General { get; set; }

        /// <summary>
        /// 红
        /// </summary>
        public ChannelCurveOption Red { get; set; }

        /// <summary>
        /// 绿
        /// </summary>
        public ChannelCurveOption Green { get; set; }

        /// <summary>
        /// 蓝
        /// </summary>
        public ChannelCurveOption Blue { get; set; }

        public ImageCurveOption()
        {
            General = new ChannelCurveOption();
            Red = new ChannelCurveOption();
            Green = new ChannelCurveOption();
            Blue = new ChannelCurveOption();
        }

        /// <summary>
        /// 指示所有通道是否是默认值
        /// </summary>
        [XmlIgnore]
        public bool IsDefault
        {
            get { return General.IsDefault 
                && Red.IsDefault  
                && Green.IsDefault 
                && Blue.IsDefault; }
        }

        public override bool Serialize()
        {
            return Serializer.SaveToXml(ConfigFilePath, this);
        }

        public override bool Load()
        {
            ImageCurveOption obj = (ImageCurveOption)Serializer.LoadFromXml(ConfigFilePath, this.GetType());
            if (obj != null)
            {
                this.General = obj.General;
                this.Red = obj.Red;
                this.Blue = obj.Blue;
                this.Green = obj.Green;              
                this._nVer = obj._nVer;
                return true;
            }
            else
            {
                ErrorMessage = Serializer.Message;
                return false;
            }
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("图像曲线调整选项: ");
            FillFieldValueStringDelegate FillString =
               (string field, object val) =>
               {
                   if (val != null)
                   {
                       builder.Append("[");
                       builder.Append(field);
                       builder.Append("]: ");
                       builder.Append(val.ToString() + "; ");
                   }
               };
            FillString("[General]:", General);
            FillString("[Red]:", Red);
            FillString("[Blue]:", Blue);
            FillString("[Green]:", Green);       
            return builder.ToString();
        }
    }

    /// <summary>
    /// 通道曲线原始控制点
    /// </summary>
    public class ChannelCurveOption
    {
        /// <summary>
        /// 原始控制点
        /// </summary>
        private List<Point> _controlPoints = new List<Point>();
        public List<Point> ControlPoints
        {
            get { return _controlPoints; }
            set { _controlPoints = value; }
        }

        /// <summary>
        /// 指示曲线是不是默认值，默认值不需要映射。
        /// </summary>
        [XmlIgnore]
        public bool IsDefault
        {
            get { return _controlPoints == null || _controlPoints.Count == 0; }
        }

        public ChannelCurveOption()
        {
            _controlPoints = new List<Point>();
        }

        public void SetControlPoints(List<Point> points)
        {
            _controlPoints.Clear();
            _controlPoints.AddRange(points);
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("曲线原始控制点集合: ");
           foreach(Point p in _controlPoints)
           {
               builder.Append("(");
               builder.Append(p.X.ToString());
               builder.Append(",");
               builder.Append(p.Y.ToString());
               builder.Append("),");
           }
           return builder.ToString();
        }
    }
}
